﻿using System;
using System.IO;
using System.Reactive.Linq;
using Wulong.Phone.Qiushi.Models;
using Wulong.Phone.Utils;

namespace Wulong.Phone.Qiushi.Helpers
{
    public enum ServiceStatus
    {
        Success,
        Failed
    }

    public static class DataAgent
    {
        /// <summary>
        /// The method chains the Observables together to allow you to fetch the latest feed periodically 
        /// </summary>
        /// <param name="section">The story category you want to get</param>
        /// <param name="timeInterval">Set the interval betweem fetches</param>
        /// <returns>the observable chained together</returns>
        public static IObservable<QiushiItem> FetchPostsBySection(string section, TimeSpan timeInterval)
        {
            return Observable
                .Interval(timeInterval)
                .Timeout(TimeSpan.FromSeconds(10)) //10 seconds timeout
                .SelectMany(x => QiushiMapper.GetObservableItems(section));
        }

        public static IObservable<QiushiItem> LoadPostsBySection(string section, int pageSize, int pageNum)
        {
            return QiushiMapper.GetObservableItems(section, pageSize, pageNum);
        }

        public static IObservable<QiushiItem> RefershPostList(string section)
        {
            return QiushiMapper.GetObservableItems(section);
        }

        public static IObservable<QiushiItem> LoadHotPictures()
        {
            return QiushiMapper.GetObservableItems("pic", 10);
        }

        public static IObservable<BingBackgroundItem> FetchBingBackground(int daysAgo = 7, string mkt = "en-US")
        {
            return BingMapper.FetchBingBackgroundItems(daysAgo, mkt);
        }

        public static IObservable<MemoryStream> FetchBackground()
        {
            return BingMapper
                .FetchBingBackgroundItems(1, "en-US")
                .SelectMany(item =>
                                {
                                    var stream = ObservableResource
                                            .FromInternet(item.ImageUrl);
                                    if(stream ==  null)
                                        throw new Exception("network error");
                                    return stream;
                                    //var stream = ObservableResource.FromCache("background");
                                    //if (stream == null)
                                    //    return ObservableResource
                                    //        .FromInternet(item.ImageUrl);
                                    //var lastDate = new DateTime(int.Parse(item.EndDate.Substring(0, 4)),
                                    //                            int.Parse(item.EndDate.Substring(4, 2)),
                                    //                            int.Parse(item.EndDate.Substring(6, 2)));
                                    //return DateTime.Now.Subtract(lastDate).Days > 1
                                    //           ? ObservableResource.CacheAsFallback(item.ImageUrl, "background")
                                    //           : stream;
                                }
                );
        }

    }
}